package me.algo.str;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Map;
import java.util.NavigableMap;
import java.util.Objects;
import java.util.Optional;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.TreeMap;

/**
 * @author guozheng
 * @date 2024/5/13
 */
public class S3 {
    public static String largestWordCount(String[] messages, String[] senders) {
        TreeMap<String, Integer> map = new TreeMap<>();
        for (int i = 0; i < messages.length; i++) {
            String msg = messages[i];
            String[] split = msg.split("\\+ ");
            int length = split.length;
            String sender = senders[i];
            Integer val = map.getOrDefault(sender, 0);
            map.put(sender, val + length);
        }
        NavigableMap<String, Integer> navigableMap = map.descendingMap();
        Map.Entry<String, Integer> entry = navigableMap.lastEntry();
        Integer value = entry.getValue();
        Optional<String> first = map.entrySet().stream().filter(en -> Objects.equals(en.getValue(), value))
                .map(Map.Entry::getKey)
                .sorted(Comparator.reverseOrder())
                .findFirst();

        return first.get();
    }


    public static void main(String[] args) {
        String[] msgs = {"tP x M VC h lmD","D X XF w V","sh m Pgl","pN pa","C SL m G Pn v","K z UL B W ee","Yf yo n V U Za f np","j J sk f qr e v t","L Q cJ c J Z jp E","Be a aO","nI c Gb k Y C QS N","Yi Bts","gp No g s VR","py A S sNf","ZS H Bi De dj dsh","ep MA KI Q Ou"};
//        String[] msgs = {"Hello userTwooo", "Hi userThree", "Wonderful day Alice", "Nice day userThree"};
        String[] ss = {"OXlq","IFGaW","XQPeWJRszU","Gb","HArIr","Gb","FnZd","FnZd","HArIr","OXlq","IFGaW","XQPeWJRszU","EMoUs","Gb","EMoUs","EMoUs"};
        System.out.println(largestWordCount(msgs, ss));

    }

}
